﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 数据接口实现；特殊气报告项，检测值
    /// </summary>
    /// 创建时间：2022-12-16 09:46:19。作者：王浩力
    public class Teshu_gas_itemDAL_impl : BaseDAL, ITeshu_gas_itemDAL
    {
        public IResult Add(Teshu_gas_item teshu_Gas_Item)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            string sql = @$"INSERT INTO teshu_gas_item (
                               id,
                               teshu_gas_id,
                               analysis_time,
                               h2s_mg_m3,
                               s_mg_m3
                           )
                           VALUES (
                               {snowflake.NextId()},
                               $teshu_gas_id,
                               $analysis_time,
                               $h2s_mg_m3,
                               $s_mg_m3
                           );
";
            string error = null;
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$teshu_gas_id", teshu_Gas_Item.Teshu_gas_id);
            keyValuePairs.Add("$analysis_time", teshu_Gas_Item.Analysis_time);
            keyValuePairs.Add("$h2s_mg_m3", teshu_Gas_Item.H2s_mg_m3);
            keyValuePairs.Add("$s_mg_m3", teshu_Gas_Item.S_mg_m3);

            int count = ExecSqlCmd(sql, keyValuePairs, c =>
            {
                return c.ExecuteNonQuery();
            }, ex => { error = ex.Message; });
            if (count > 0)
            {
                return new ResultSuccess("新增成功");
            }
            return new Result("新增异常" + error);
        }

        public IResult Delete(long teshu_Gas_ItemKEY)
        {
            string sql = $"delete from  teshu_Gas_Item  where id={teshu_Gas_ItemKEY} ";
            int count = ExecSqlCmd(sql, null, c =>
            {
                return c.ExecuteNonQuery();
            });
            if (count > 0)
            {
                return new ResultSuccess("删除成功");
            }
            return new ResultSuccess("删除失败");
        }

        public IResult Edit(Teshu_gas_item teshu_Gas_Item)
        {
            string sql = @$"UPDATE  teshu_gas_item
   SET
       teshu_gas_id = $teshu_gas_id,
       analysis_time = $analysis_time,
       h2s_mg_m3 = $h2s_mg_m3,
       s_mg_m3 = $s_mg_m3
 WHERE id ={teshu_Gas_Item.Id} ";
            string error = null;
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$teshu_gas_id", teshu_Gas_Item.Teshu_gas_id);
            keyValuePairs.Add("$analysis_time", teshu_Gas_Item.Analysis_time);
            keyValuePairs.Add("$h2s_mg_m3", teshu_Gas_Item.H2s_mg_m3);
            keyValuePairs.Add("$s_mg_m3", teshu_Gas_Item.S_mg_m3);

            int count = ExecSqlCmd(sql, keyValuePairs, c =>
            {
                return c.ExecuteNonQuery();
            }, ex => { error = ex.Message; });
            if (count > 0)
            {
                return new ResultSuccess("修改成功");
            }
            return new Result("修改异常" + error);
        }

        public List<Teshu_gas_item> GetList(long teshu_gas_id)
        {
            string sql = "select  *  from  Teshu_gas_item where  teshu_gas_id=" + teshu_gas_id;
            return ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Teshu_gas_item> list = new List<Teshu_gas_item>();

                while (reader.Read())
                {
                    Teshu_gas_item info = new Teshu_gas_item();
                    info.Id = reader.GetInt64(0);
                    info.Teshu_gas_id = (long)reader["Teshu_gas_id"].ToMyInt64();
                    info.Analysis_time = reader["Analysis_time"].ToString();
                    info.H2s_mg_m3 = reader["h2s_mg_m3"].ToMyDecimal();
                    info.S_mg_m3 = reader["s_mg_m3"].ToMyDecimal();
                    list.Add(info);
                }
                return list;
            });

        }
    }
}
